package com.yyc.system.controller;
import com.yyc.core.Result;
import com.yyc.core.ResultGenerator;
import com.yyc.system.model.TUserBook;
import com.yyc.system.service.ITUserBookService;

import tk.mybatis.mapper.entity.Condition;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* Created by yyc on 2018/12/26.
*/
@RestController
@RequestMapping("/t/user/book")
public class TUserBookController {
    @Autowired
    private ITUserBookService tUserBookService;

    @RequestMapping("/add")
    public Result add(@RequestBody TUserBook tUserBook) {
        tUserBookService.save(tUserBook);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        tUserBookService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody TUserBook tUserBook) {
        tUserBookService.update(tUserBook);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        TUserBook tUserBook = tUserBookService.findById(id);
        return ResultGenerator.genSuccessResult(tUserBook);
    }

    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TUserBook> list = tUserBookService.findAll();
        PageInfo<TUserBook> pageInfo = new PageInfo<TUserBook>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    
    //*********************************************************************
    @RequestMapping("/queryUserReadBook")
    public Result queryUserReadBook(Integer userId) {
    	List<TUserBook> userBooks = tUserBookService.findUserReadBook(userId);
    	return ResultGenerator.genSuccessResult(userBooks);
    }
}
